package top.codedance.iotp.tools.terminal.ui;

import top.codedance.iotp.tools.terminal.TerminalApplication;

import javax.swing.*;
import java.net.URL;

/**
 *
 * @author xuedianfu
 */
public class WaitLoading extends javax.swing.JDialog {

    private long time = 0;

    /**
     * Creates new form WaitLoading
     */
    public WaitLoading(JDialog owner) {
        super(owner, true);
        init();
    }

    public WaitLoading(JFrame owner) {
        super(owner, true);
        init();
    }

    private void init(){
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(this.getOwner());
        pack();
        setTitle("代码飞舞采控系统-等待处理中...");

        TerminalApplication.pool.submit(() -> {
            int i = 0;
            while (true){
                if(WaitLoading.this.isVisible()){
                    if(i > 20){
                        WaitLoading.this.close();
                        JOptionPane.showMessageDialog(rootPane, "长时间无返回，可能是命令本身没有返团成员值，或服务端无响应。");
                    }
                    i++;
                }else{
                    i = 0;
                }
                try{
                    Thread.sleep(500);
                }catch (Exception e){}
            }
        });
    }

    public void open(){
        setLocationRelativeTo(this.getOwner());
        new Thread(() -> {setVisible(true);}).start();
        time = System.currentTimeMillis();
    }

    public void close(){
        if(System.currentTimeMillis() - time < 500){
            try {
                Thread.sleep(500);
            }catch (Exception e){}
        }
        setVisible(false);
        dispose();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jpb_wait = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("处理中:");

        jpb_wait.setIndeterminate(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jpb_wait, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpb_wait, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JProgressBar jpb_wait;
    // End of variables declaration//GEN-END:variables
}
